package com.tepth.maintenancesystem.presenter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tepth.maintenancesystem.R;
import com.tepth.maintenancesystem.bean.Contents;
import com.tepth.maintenancesystem.bean.MaintainListWork;
import com.tepth.maintenancesystem.bean.MaintainRefer;
import com.tepth.maintenancesystem.untils.Untils;

import java.util.List;

/**
 * Created by maning on 16/1/25.
 */
public class MaintainDetailsRightAdapter extends BaseAdapter {

    private List<Contents> mDatas;
    private Context mContext;
    private LayoutInflater layoutInflater;
    private MaintainDetailsRightOnClick rightOnClick;
    private List<MaintainRefer> refers;


    public MaintainDetailsRightAdapter(Context mContext, List<Contents> mDatas, MaintainDetailsRightOnClick rightOnClick) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        layoutInflater = LayoutInflater.from(this.mContext);
        this.rightOnClick = rightOnClick;
        refers = Untils.getMaintianRefer(mContext);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.maintain_details_right_item, parent, false);
            viewHolder.tv_right_details = (TextView) convertView.findViewById(R.id.tv_right_details);
            viewHolder.img_details_right = (ImageView) convertView.findViewById(R.id.img_details_right);
            viewHolder.rl_right_details = (RelativeLayout) convertView.findViewById(R.id.rl_right_details);
            viewHolder.tv_right_details_value = (TextView) convertView.findViewById(R.id.tv_right_details_value);
            viewHolder.tv_right_details_name = (TextView) convertView.findViewById(R.id.tv_right_details_name);
            viewHolder.rl_tv_details = (RelativeLayout) convertView.findViewById(R.id.rl_tv_details);
            viewHolder.linear_details_right_item= (LinearLayout) convertView.findViewById(R.id.linear_details_right_item);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final Contents object = mDatas.get(position);
        String value = "";
        if (object.getValueTypeCode().equals("text")) {
            value = object.getValue();
            viewHolder.rl_right_details.setVisibility(View.GONE);
        } else {
            if (object.getValueTypeCode().equals("select")) {
                value = getSelectValue(object.getValueTypeDetail(), object.getValue());
            } else {
                value = object.getValue();
            }
            viewHolder.rl_right_details.setVisibility(View.VISIBLE);
        }
//        viewHolder.tv_right_details.setBackgroundColor(Untils.getColor(mContext, R.color.colorSecondItemBackground));
        viewHolder.tv_right_details.setTextColor(Untils.getColor(mContext, R.color.colorItemTextBlack));
        viewHolder.tv_right_details.setText(object.getContentDesc());
        if(object.getTemplateId()==0){
            viewHolder.linear_details_right_item.setBackgroundColor(Untils.getColor(mContext,R.color.colorSecondItemBackground));
        }else{
            if(Untils.getLastString(object.getMaintenanceTypeCode()).equals("null")){
                viewHolder.linear_details_right_item.setBackgroundColor(Untils.getColor(mContext,R.color.colorSecondItemBackground));
            }else if(Untils.getLastString(object.getMaintenanceTypeCode()).equals("2")){
                viewHolder.linear_details_right_item.setBackgroundColor(Untils.getColor(mContext,R.color.contents_two));
            }else if(Untils.getLastString(object.getMaintenanceTypeCode()).equals("3")){
                viewHolder.linear_details_right_item.setBackgroundColor(Untils.getColor(mContext,R.color.contents_three));
            }
        }
        if (object.getWorkPersonName().equals("null")||object.getWorkPersonName().equals("")) {
            viewHolder.tv_right_details_name.setVisibility(View.GONE);
            if (object.getValue().equals("")) {
                viewHolder.tv_right_details_value.setVisibility(View.GONE);
                viewHolder.tv_right_details_name.setVisibility(View.GONE);
                viewHolder.img_details_right.setImageDrawable(Untils.getDrawable(mContext, R.drawable.details_right_grey));
            } else {
                viewHolder.tv_right_details_value.setVisibility(View.VISIBLE);
                viewHolder.tv_right_details_value.setText(value);
                viewHolder.img_details_right.setImageDrawable(Untils.getDrawable(mContext, R.drawable.details_right_red));
            }
        } else {
            viewHolder.tv_right_details_value.setVisibility(View.VISIBLE);
            viewHolder.tv_right_details_name.setVisibility(View.VISIBLE);
            viewHolder.tv_right_details_value.setText(value);
            viewHolder.tv_right_details_name.setText(object.getWorkPersonName());
            viewHolder.img_details_right.setImageDrawable(Untils.getDrawable(mContext, R.drawable.details_right_green));
        }
        viewHolder.rl_right_details.setTag(viewHolder.rl_tv_details);
        viewHolder.rl_right_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageView img = (ImageView) view.findViewById(R.id.img_details_right);
                RelativeLayout rl_text = (RelativeLayout) view.getTag();
                TextView tv_details_value = (TextView) rl_text.findViewById(R.id.tv_right_details_value);
                TextView tv_details_name = (TextView) rl_text.findViewById(R.id.tv_right_details_name);
                TextView tv_right_details = (TextView) rl_text.findViewById(R.id.tv_right_details);
                //rightOnClick.checkboxOnClick(object,0);
                if (tv_right_details.getCurrentTextColor() == Untils.getColor(mContext, R.color.colorPrimary)) {//type=2
                    rightOnClick.checkboxOnClick(object,2);
                    tv_details_value.setVisibility(View.VISIBLE);
                    tv_details_name.setVisibility(View.VISIBLE);
                    String value = "";
                    if (object.getValueTypeCode().equals("select")) {
                        value = getSelectValue(object.getValueTypeDetail(), object.getValue());
                    } else {
                        value = object.getValue();
                    }
                    tv_details_value.setText(value);
                    tv_details_name.setText(object.getWorkPersonName());
                    tv_right_details.setTextColor(Untils.getColor(mContext, R.color.colorItemTextBlack));
                    img.setImageDrawable(Untils.getDrawable(mContext, R.drawable.details_right_green));
                } else {
                    if (object.getValue().equals("")) {//变红，type=0
                        rightOnClick.checkboxOnClick(object,0);
                        object.setValue(object.getValueDefault());
                        tv_details_value.setVisibility(View.VISIBLE);
                        String value = "";
                        if (object.getValueTypeCode().equals("select")) {
                            value = getSelectValue(object.getValueTypeDetail(), object.getValue());
                        } else {
                            value = object.getValue();
                        }
                        tv_details_value.setText(value);
                        tv_right_details.setTextColor(Untils.getColor(mContext, R.color.colorItemTextBlack));
                        img.setImageDrawable(Untils.getDrawable(mContext, R.drawable.details_right_red));
                    } else {
                        if (object.getWorkPersonName().equals("null")||object.getWorkPersonName().equals("")) {//红变灰type=1
                            rightOnClick.checkboxOnClick(object,1);
                            tv_right_details.setTextColor(Untils.getColor(mContext, R.color.colorItemTextBlack));
                            object.setValue("");
                        } else {//绿变回灰色，type=3
                            rightOnClick.checkboxOnClick(object,3);
                            tv_right_details.setTextColor(Untils.getColor(mContext, R.color.colorPrimary));
                        }
                        tv_details_name.setVisibility(View.GONE);
                        tv_details_name.setText("");
                        tv_details_value.setVisibility(View.GONE);
                        tv_details_value.setText("");
                        img.setImageDrawable(Untils.getDrawable(mContext, R.drawable.details_right_grey));
                    }
                }
            }
        });
        viewHolder.rl_tv_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rightOnClick.textOnClick(object);
            }
        });
        return convertView;
    }

    private String getSelectValue(String valueTypeDetail, String obj_value) {
        String value = "";
        for (int i = 0; i < refers.size(); i++) {
            if (refers.get(i).getSelectType().equals(valueTypeDetail)) {
                List<MaintainRefer.Selection> selections = refers.get(i).getSelections();
                for (MaintainRefer.Selection selection : selections) {
                    if (selection.getCode().equals(obj_value)) {
                        value = selection.getName();
                        break;
                    }
                }
            }
        }
        return value;
    }

    public void setDatas(List<Contents> mDatas) {
        this.mDatas = mDatas;
    }

    private static class ViewHolder {
        TextView tv_right_details, tv_right_details_value, tv_right_details_name;
        ImageView img_details_right;
        RelativeLayout rl_right_details, rl_tv_details;
        LinearLayout linear_details_right_item;
    }

}
